﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business;
using blackhouse.JavaScript.JQuery;
using blackhouse.Web.Controls;

namespace blackhouse.WebNemo.Web {
    public partial class WebNemo : System.Web.UI.MasterPage {

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Factory.Instance().GetCurrentUser().HasSufficientPermissions(true);
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (!string.IsNullOrEmpty(Request["mod"]) && Request["mod"].ToLower() == "fullw") {
                this.body.Attributes.Add("class", "full-width");
                this.groups.Visible = false;
                this.lnkCreateGroup.Visible = false;
            }
            NemoJsLibrary js = new NemoJsLibrary();
            js.IncludePopup();
            js.IncludeResizer();
            js.IncludeScroller();

            this.ShowMessageIfNeeded();
        }

        private void ShowMessageIfNeeded() {
            if (!string.IsNullOrEmpty(Request["message"]))
                this.PopulateMessage(Request["message"]);
        }

        private void PopulateMessage(string message) {
            Notification notification = new Notification();
            notification.Text = this.GetMessageResourceString(message + ".Text");
            notification.Title = this.GetMessageResourceString(message + ".Title");
            string notType = this.GetMessageResourceString(message + ".Type");
            if (Enum.GetNames(typeof(NotificationType)).Contains(notType)) {
                NotificationType notificationType = (NotificationType)Enum.Parse(typeof(NotificationType), notType);
                notification.NotificationType = notificationType;
            }
            notification.TimeoutShow = 6000;
            notification.UseInternalStyle = false;
            this.form1.Controls.Add(notification);
        }

        private string GetMessageResourceString(string key) {
            object obj = GetGlobalResourceObject("Messages", key);
            return (obj == null) ? String.Empty : obj.ToString();
        }

    }
}